package net.adst.searcher;

import net.adst.beans.NaemTec;
import net.adst.beans.OurTecRow;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.util.DataManager;
import net.adst.util.MyUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 12.11.12
 * Time: 9:42
 * To change this template use File | Settings | File Templates.
 */
public class Searcher {

    public static void searchNaem() {
        ArrayList<NaemTec> naemTecs = (ArrayList<NaemTec>) DataManager.getObject(VariableNames.TEC_NAEM_ALL);
        String tecName = ((JComboBox) DataManager.getObject(VariableNames.UI_NAEM_TECNAMECOMBO)).getSelectedItem().toString();
        String manager = ((JComboBox) DataManager.getObject(VariableNames.UI_NAEM_MANAGERCOMBO)).getSelectedItem().toString();
        String customer = ((JComboBox) DataManager.getObject(VariableNames.UI_NAEM_CUSTOMERCOMBO)).getSelectedItem().toString();
        String performer = ((JComboBox) DataManager.getObject(VariableNames.UI_NAEM_PERFORMERCOMBO)).getSelectedItem().toString();
        boolean paidNam = ((JCheckBox) DataManager.getObject(VariableNames.UI_NAEM_PAIDNAM_CHECK)).isSelected();
        boolean unPaidNam = ((JCheckBox) DataManager.getObject(VariableNames.UI_NAEM_UNPAIDNAM_CHECK)).isSelected();
        boolean paidNado = ((JCheckBox) DataManager.getObject(VariableNames.UI_NAEM_PAIDNADO_CHECK)).isSelected();
        boolean paidOk = ((JCheckBox) DataManager.getObject(VariableNames.UI_NAEM_PAIDOK_CHECK)).isSelected();
        boolean unPaid = ((JCheckBox) DataManager.getObject(VariableNames.UI_NAEM_UNPAID_CHECK)).isSelected();
//        JComboBox namesTecCombo = (JComboBox) DataManager.getObject(VariableNames.UI_NAEM_TECNAMECOMBO);
//        MyUtils.setComboMenuWidth(namesTecCombo, namesTecCombo.getFontMetrics(namesTecCombo.getFont()));
        ArrayList<Integer> paidInf = new ArrayList<Integer>(4);
        paidInf.add(-1);
        if (paidNado) {
            paidInf.add(1);
        }
        if (paidOk) {
            paidInf.add(2);
        }
        if (unPaid) {
            paidInf.add(0);
        }

        DefaultTableModel detailModel = (DefaultTableModel) DataManager.getObject(VariableNames.UI_NAEM_DETAILED_MODEL);
        ArrayList<NaemTec> usingNaemRows = new ArrayList();
        detailModel.setRowCount(0);
        Boolean showForAllTime = (Boolean) DataManager.getObject(VariableNames.TOP_PANEL_ALL_TIME);
        if (naemTecs != null) {
            for (NaemTec tec : naemTecs) {
                if (!showForAllTime) {
                    if ((MyUtils.checkDates(tec.getDate())) && ((tecName.equals(tec.getTecName())) || (tecName.equals(Constants.ALL_TEC))) && ((manager.equals(tec.getManagerName())) || (manager.equals(Constants.ALL_MANAGERS))) && ((customer.equals(tec.getCustomer())) || (customer.equals(Constants.ALL_CUSTOMERS))) && ((performer.equals(tec.getPerformer())) || (performer.equals(Constants.ALL_PERFORMERS))) && ((tec.getPaidNam() && paidNam) || (!tec.getPaidNam() && unPaidNam)) && (paidInf.contains(tec.getMoneyIn()))) {
                        detailModel.addRow(tec.toRow());
                        usingNaemRows.add(tec);
                    }
                } else {
                    if (((tecName.equals(tec.getTecName())) || (tecName.equals(Constants.ALL_TEC))) && ((manager.equals(tec.getManagerName())) || (manager.equals(Constants.ALL_MANAGERS))) && ((customer.equals(tec.getCustomer())) || (customer.equals(Constants.ALL_CUSTOMERS))) && ((performer.equals(tec.getPerformer())) || (performer.equals(Constants.ALL_PERFORMERS))) && ((tec.getPaidNam() && paidNam) || (!tec.getPaidNam() && unPaidNam)) && (paidInf.contains(tec.getMoneyIn()))) {
                        detailModel.addRow(tec.toRow());
                        usingNaemRows.add(tec);
                    }
                }
            }
            DataManager.setObject(VariableNames.TEC_NAEM_USING_ROWS, usingNaemRows);
        }
        DefaultTableModel sumModel = (DefaultTableModel) DataManager.getObject(VariableNames.UI_NAEM_SUM_MODEL);
        sumModel.setRowCount(0);
        Double[] sumRow = new Double[]{0.0, 0.0, 0.0, 0.0, 0.0};
        if (!usingNaemRows.isEmpty()) {
            for (NaemTec tec : usingNaemRows) {
                if (!tec.getHours().isEmpty()) {
                    sumRow[0] += Double.valueOf(tec.getHours());
                }
                if (!tec.getDohod().isEmpty()) {
                    sumRow[1] += Double.valueOf(tec.getDohod());
                }
                if (!tec.getHoursOur().isEmpty()) {
                    sumRow[2] += Double.valueOf(tec.getHoursOur());
                }
                if (!tec.getRashod().isEmpty()) {
                    sumRow[3] += Double.valueOf(tec.getRashod());
                }
                if (!tec.getProfit().isEmpty()) {
                    sumRow[4] += Double.valueOf(tec.getProfit());
                }
            }

            //String.valueOf(new BigDecimal(dtCost).setScale(2, RoundingMode.HALF_DOWN))
            sumModel.addRow(new Object[]{sumRow[0], String.valueOf(new BigDecimal(sumRow[1]).setScale(2, RoundingMode.HALF_DOWN)), sumRow[2], String.valueOf(new BigDecimal(sumRow[3]).setScale(2, RoundingMode.HALF_DOWN)), String.valueOf(new BigDecimal(sumRow[4]).setScale(2, RoundingMode.HALF_DOWN))});
//            sumModel.addRow(sumRow);
        } else {
            sumModel.addRow(new Object[]{"", "", "", ""});
        }
    }

    public static void searchOur() {
        ArrayList<OurTecRow> ourTecs = (ArrayList<OurTecRow>) DataManager.getObject(VariableNames.TEC_OUR_ALL);
        String tecName = ((JComboBox) DataManager.getObject(VariableNames.UI_OUR_TECNAMECOMBO)).getSelectedItem().toString();
        String surname = ((JComboBox) DataManager.getObject(VariableNames.UI_OUR_SURNAMECOMBO)).getSelectedItem().toString();
        String customer = ((JComboBox) DataManager.getObject(VariableNames.UI_OUR_CUSTOMERCOMBO)).getSelectedItem().toString();
        boolean paidFor = ((JCheckBox) DataManager.getObject(VariableNames.UI_OUR_PAIDFOR_CHEK)).isSelected();
        boolean unpaidFor = ((JCheckBox) DataManager.getObject(VariableNames.UI_OUR_UNPAIDFOR_CHEK)).isSelected();
        DefaultTableModel detailModel = (DefaultTableModel) DataManager.getObject(VariableNames.UI_OUR_DETAILED_MODEL);
        ArrayList<OurTecRow> usingOurRows = new ArrayList();
        detailModel.setRowCount(0);
        Boolean showForAllTime = (Boolean) DataManager.getObject(VariableNames.TOP_PANEL_ALL_TIME);
        if (ourTecs != null) {
            for (OurTecRow tec : ourTecs) {
                boolean tecPaid = tec.getPaidFor();
                if (!showForAllTime) {
                    if ((MyUtils.checkDates(tec.getDate())) && ((tecName.equals(tec.getShortName())) || (tecName.equals(Constants.ALL_TEC))) && ((surname.equals(tec.getSureName())) || (surname.equals(Constants.ALL_SURNAME))) && ((customer.equals(tec.getCustomer())) || (customer.equals(Constants.ALL_CUSTOMERS))) && ((tecPaid && paidFor) || ((!tecPaid) && unpaidFor))) {
                        detailModel.addRow(tec.toRow());
                        usingOurRows.add(tec);
                    }
                } else {
                    if (((tecName.equals(tec.getShortName())) || (tecName.equals(Constants.ALL_TEC))) && ((surname.equals(tec.getSureName())) || (surname.equals(Constants.ALL_SURNAME))) && ((customer.equals(tec.getCustomer())) || (customer.equals(Constants.ALL_CUSTOMERS))) && ((tecPaid && paidFor) || ((!tecPaid) && unpaidFor))) {
                        detailModel.addRow(tec.toRow());
                        usingOurRows.add(tec);
                    }
                }
            }
            DataManager.setObject(VariableNames.TEC_OUR_USING_ROWS, usingOurRows);
        }
        DefaultTableModel sumModel = (DefaultTableModel) DataManager.getObject(VariableNames.UI_OUR_SUM_MODEL);
        sumModel.setRowCount(0);
        Double[] sumRow = new Double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
        if (!usingOurRows.isEmpty()) {
            for (OurTecRow tec : usingOurRows) {
                if (!tec.getHoursFacts().isEmpty()) {
                    sumRow[0] += Double.valueOf(tec.getHoursFacts());
                }
                if (!tec.getHours().isEmpty()) {
                    sumRow[1] += Double.valueOf(tec.getHours());
                }
                if (!tec.getProfit().isEmpty()) {
                    sumRow[2] += Double.valueOf(tec.getProfit());
                }
                if (!tec.getDtAmount().isEmpty()) {
                    sumRow[3] += Double.valueOf(tec.getDtAmount());
                }
                if (!tec.getDtCost().isEmpty()) {
                    sumRow[4] += Double.valueOf(tec.getDtCost());
                }
                if (!tec.getSalary().isEmpty()) {
                    sumRow[5] += Double.valueOf(tec.getSalary());
                }
                if (!tec.getDopTraty().isEmpty()) {
                    sumRow[6] += Double.valueOf(tec.getDopTraty());
                }
                if (!tec.getDohod().isEmpty()) {
                    sumRow[7] += Double.valueOf(tec.getDohod());
                }
                if (!tec.getArenda().isEmpty()) {
                    sumRow[8] += Double.valueOf(tec.getArenda());
                }
                if (!tec.getProrab().isEmpty()) {
                    sumRow[9] += Double.valueOf(tec.getProrab());
                }
            }
            sumRow[4] = Double.valueOf(new BigDecimal(sumRow[4]).setScale(2, RoundingMode.HALF_DOWN).toString());
            sumRow[7] = Double.valueOf(new BigDecimal(sumRow[7]).setScale(2, RoundingMode.HALF_DOWN).toString());
//            sumModel.addRow(new Object[]{sumRow[0], sumRow[1], sumRow[2], sumRow[3]});
            sumModel.addRow(sumRow);
        } else {
            sumModel.addRow(new Object[]{"", "", "", ""});
        }

    }

}
